<?php
// No direct access to this file
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

class ManagerViewManager extends JView {
	
	var $table = null;
	
    function __construct() {
    	ManagerHelper::showMenuPim("manager");
        $this->table =& JTable::getInstance('Serial', 'Table');	

        $Serial = JModel::getInstance('Serial', 'PimModel');
		$arr_status = $Serial->getListStatus();
		
        $Pond = JModel::getInstance('Pond', 'PimModel');
		$arr_pond = $Pond->getListSelectBox();
        
        $lists['pond_code'] = JHTML::_('select.genericlist', $arr_pond, "pond_code", "class='inputbox'", 'code', 'name');
        $lists['type'] = JHTML::_('select.genericlist', ManagerHelper::$SERIAL_TYPE, "status", "class='inputbox'",'id', 'name');
        
		$this->assign('arr_status', $arr_status);
        $this->assign('arr_pond', $arr_pond);
        $this->assign('lists', $lists);
		
        parent::__construct();
    }	
    
	function display() {
		$mainframe = JFactory::getApplication();
        $this->setLayout('display');
        
        $post = JRequest::get('post', JREQUEST_ALLOWHTML);
        if (isset($post['change_type_ready'])) {
        	$cid = JRequest::getVar('cid');
            $pondCode = JRequest::getVar('pond_code');
                        
            if (!empty($cid) && $pondCode) {
                foreach ($cid as $serialID) {
                    $serial = JModel::getInstance('Serial', 'PimModel');
                    $result = $serial->updateType($serialID, ManagerHelper::SERIAL_TYPE_READY, $pondCode, ManagerHelper::SERIAL_BELONG_POND, FALSE);
                }
            }
        }
        
        $where = $this->filter($mainframe);
        
        ManagerHelper::showToolBar('Serial Management', true, true);
        						
        $limit				= $mainframe->getUserStateFromRequest( 'serials.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
        $limitstart 		= $mainframe->getUserStateFromRequest( 'serials.limitstart', 'limitstart', 0, 'int' );
        $filter_order		= $mainframe->getUserStateFromRequest( 'serials.filter_order', 'filter_order', 'id', 'cmd' );
        $filter_order_Dir	= $mainframe->getUserStateFromRequest( 'serials.filter_order_Dir', 'filter_order_Dir', ManagerHelper::ORDER_DIR, 'word' );
      
        $Model = JModel::getInstance('Serial', 'PimModel');
        $Model->getData($total, $dbrow, $filter_order, $filter_order_Dir, $limitstart, $limit, $where);
		
        jimport('joomla.html.pagination');
        $pageNav = new JPagination( $total, $limitstart, $limit );
       
        $this->assign('TOTAL_ROW', $total);
        $this->assign('dbrow', $dbrow);
        $this->assign('LIST_FOOTER_PAV', $pageNav->getListFooter());
        $this->assign('FORM_ORDER', $filter_order);
        $this->assign('FORM_ORDER_DIR',$filter_order_Dir);
      
        parent::display();
    }
    
 	function addForm() {		
		$mainframe = JFactory::getApplication();
		$this->setLayout('form');					
		$cid = JRequest::getVar('cid');
	    
        if(!empty($cid)) {
			$rows = $this->table;
			$rows->load($cid[0]);	
            
            $lists['type'] = JHTML::_('select.genericlist', ManagerHelper::$SERIAL_TYPE, "type", "class='inputbox'",'id', 'name', intval($rows->type));
            
            $this->assign('lists', $lists);
			$this->assign('rows', $rows);
		}
		
		parent::display();
    }
    
    function filter($mainframe) {
    	$where = array();
 		$filter_serial = $mainframe->getUserStateFromRequest('filter.serials.code', 'filter_serial', '', 'cmd');
 		$filter_status = $mainframe->getUserStateFromRequest('filter.serials.status', 'filter_status', '', 'int');
        $filter_type = $mainframe->getUserStateFromRequest('filter.serials.type', 'filter_type', '', 'int');
        $filter_pond = $mainframe->getUserStateFromRequest('filter.serials.pond', 'filter_pond', '', 'int');
 		
    	if ($filter_serial) {
 			//$where[] = 'code LIKE "%' . $filter_serial . '%"';
 			$where[] = "batch=(SELECT batch FROM #__serial WHERE `code`='$filter_serial' AND `order`=1)";
 		}
 		if ($filter_status !== '' && $filter_status != -1) {
 			$where[] = "status=$filter_status";
 		}
        if ($filter_type !== '' && $filter_type != -1) {
 			$where[] = "type=$filter_type";
 		}
        if ($filter_pond) {
 			$where[] = "item=$filter_pond&belong_to='".ManagerHelper::SERIAL_BELONG_POND."'";
 		}
 		
 		$mainframe->setUserState('filter_serials', $where);
 		
 		$this->assign('filter_serial', $filter_serial);
 		$this->assign('filter_status', $filter_status);
        $this->assign('filter_type', $filter_type);
        $this->assign('filter_pond', $filter_pond);
 		
 		return $where;
    }
}
